/**
 * 
 */
package com.taobao.wolfwolf.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.Projections;

import com.taobao.wolfwolf.hibernate.dao.ProductDao;
import com.taobao.wolfwolf.hibernate.pojo.Product;
import com.taobao.wolfwolf.util.HqlParam;
import com.taobao.wolfwolf.util.Util;

/**
 * @author Tux
 *
 * 创建时间: 2011-5-20 上午09:50:03
 */
@SuppressWarnings("unchecked")
public class ProductDaoImpl extends BaseDaoImpl implements ProductDao {

	public Product findById(int id) throws Exception {
		return (Product) getHibernateTemplate().get(Product.class, id);
	}
	
	@Override
	public Product findById(boolean lazy, int id) throws Exception {
		Product product = findById(id);
		if(product != null) {
			Hibernate.initialize(product.getTypes());
			Hibernate.initialize(product.getCcolors());
			Hibernate.initialize(product.getSpeces());
			return product;
		}
		return null;
	}
	
	public List<Product> findByName(String name) throws Exception {
		return super.findByProperty(Product.class, "name", name);
	}

	public List<Product> findAll() throws Exception {
		return getHibernateTemplate().find("from Product");
	}

	public long getTotal() throws Exception {
		return super.countByCriteria(Product.class);
	}
	
	public List<Product> paging(int page, int limit) throws Exception {
		return super.findByCriteriaByPage(Product.class, page, limit);
	}
	
	public List<Product> findByPinyin(String abbr) throws Exception {
		return super.findByProperty(Product.class, "pinyin", abbr, 3);
	}

	@Override
	public List<Product> paging(int page, int limit, List<HqlParam> param) throws Exception {
		
		Criteria criteria = getSession().createCriteria(Product.class);
		
		for(HqlParam hp : param) {
			Util.operator(criteria, hp);
		}
		
		criteria.setFirstResult(page);
		criteria.setMaxResults(limit);
		return criteria.list();
	}

	@Override
	public List<Product> findAll(List<HqlParam> param) throws Exception {
		Criteria criteria = getSession().createCriteria(Product.class);
		
		for(HqlParam hp : param) {
			Util.operator(criteria, hp);
		}
		return criteria.list();
	}

	@Override
	public long getTotal(List<HqlParam> param) throws Exception {
		Criteria criteria = getSession().createCriteria(Product.class);
		
		for(HqlParam hp : param) {
			Util.operator(criteria, hp);
		}
		criteria.setProjection(Projections.rowCount());
		return (Long) criteria.list().get(0);
	}

	@Override
	public Product findByNumIId(long num_iid) throws Exception {
		List<Product> list = findByProperty(Product.class, "num_iid", num_iid);
		if(list.size() > 0)
			return (Product) list.get(0);
		return null;
	}

	@Override
	public void removeAll() throws Exception{
		removeList(findAll());
	}

	@Override
	public Integer getMaxId() throws Exception {
		return (Integer) getSession().createQuery("select max(id) from Product").uniqueResult();
	}
}
